package atm;

import lovo01.ATM04.FileUtil;
import lovo01.ATM04.Users;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;

/**
 * GUI版本
 */
public class ATM05 extends JFrame {
    public static final String FILE_PATH = "uses01.dat";
    private ArrayList users;
    private Users loginUser = new Users();
    private Image bgImg = new ImageIcon("images/background.jpg").getImage();
    private CardLayout cardLayout = new CardLayout();
    private LoginPanel loginPanel = new LoginPanel(this);
    private MenuPanel menuPanel = new MenuPanel(this);
    private ChangePwdPanel changePwdPanel = new ChangePwdPanel(this);
    private GetPanel getPanel = new GetPanel(this);
    private SavePanel savePanel = new SavePanel(this);
    private SearchPanel searchPanel = new SearchPanel(this);
    private TransferPanel transferPanel = new TransferPanel(this);
    public ATM05(){
        into();
        this.setContentPane(new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(bgImg,0,0,this);
            }
        });
        this.setLayout(cardLayout);

        this.add(loginPanel,"login");
        this.add(menuPanel,"menu");
        this.add(savePanel,"save");
        this.add(searchPanel,"search");
        this.add(getPanel,"get");
        this.add(changePwdPanel,"changePwd");
        this.add(transferPanel,"transfer");


        this.setTitle("ATM");
        this.setSize(1024,768);
        this.setResizable(false);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    public void into(){
        users = (ArrayList<Users>) FileUtil.readObject(FILE_PATH);
        if (users == null){
            users = new ArrayList<>();
            users.add(new Users("aaa","111111",1111));
            users.add(new Users("bbb","222222",2222));
            users.add(new Users("ccc","333333",3333));
            users.add(new Users("ddd","444444",4444));
            users.add(new Users("aaa","555555",5555));
            FileUtil.writeObject(users,FILE_PATH);
        }
    }

    public void loginInto(){
            loginPanel.getAccountTextField().setText(null);
            loginPanel.getPwdField().setText(null);
            loginPanel.setErrorCount(3);
    }



    public ArrayList<Users> getUsers() {
        return users;
    }

    public void setUsers(ArrayList users) {
        this.users = users;
    }

    public Users getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(Users loginUser) {
        this.loginUser = loginUser;
    }

    public void showPanel(String name){
        cardLayout.show(this.getContentPane(),name);
    }

    public SearchPanel getSearchPanel() {
        return searchPanel;
    }

    public void setSearchPanel(SearchPanel searchPanel) {
        this.searchPanel = searchPanel;
    }

    public static void main(String[] args) {
        new ATM05();
    }
}
